package top.canyue.word_range_service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.canyue.word_range.exception.BaseException;
import top.canyue.word_range.pojo.dto.ConditionDto;
import top.canyue.word_range.pojo.po.Condition;
import top.canyue.word_range.pojo.vo.ConditionVo;
import top.canyue.word_range.result.Result;
import top.canyue.word_range_service.mapper.ConditionMapper;
import top.canyue.word_range_service.mapper.RelEventConditionMapper;
import top.canyue.word_range_service.service.ConditionService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ConditionServiceImpl extends ServiceImpl<ConditionMapper, Condition> implements ConditionService {
    final ConditionMapper conditionMapper;
    final RelEventConditionMapper relEventConditionMapper;

    @Override
    public Result<Condition> addCondition(ConditionDto conditionDto) {
        if(conditionMapper.addCondition(conditionDto.toPo()) > 0){
            return Result.success(conditionDto.toPo());
        }
        else throw new BaseException("添加条件失败");
    }

    @Override
    public Result<Condition> updateCondition(ConditionDto conditionDto) {
        if(conditionMapper.updateById(conditionDto.toPo()) > 0){
            return Result.success(conditionDto.toPo());
        }
        else throw new BaseException("更新条件失败");
    }

    @Override
    public Result<Condition> deleteCondition(String conditionId) {
        if(conditionMapper.deleteById(conditionId) > 0){
            return Result.success(conditionMapper.selectById(conditionId));
        }
        else throw new BaseException("删除条件失败");
    }

    @Override
    public Result<Condition> associatedCondition(String conditionId, String eventId) {
        if(relEventConditionMapper.associatedCondition(conditionId, eventId) > 0){
            return Result.success();
        }
        else throw new BaseException("活动与条件间关联失败");
    }

    @Override
    public Result<List<ConditionVo>> getConditionListByEvent(String eventId) {
        List<ConditionVo> conditionVoList = relEventConditionMapper.getConditionListByEvent(eventId);
        if(conditionVoList != null){
            return Result.success(conditionVoList);
        }
        else throw new BaseException("获取活动的达成条件失败");
    }
}
